<?php

/**
 * register actions.
 *
 * @package    namasce
 * @subpackage register
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class registerActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('register', 'step1');
  }

  public function executeStep1(sfWebRequest $request)
  {
    $this->form = new sfGuardRegisterForm();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('user'));
      if ($this->form->isValid())
      {
        $this->form->save();
        $this->getUser()->setFlash('new_user_id', $this->form->getObject()->id, true);
        $this->redirect('@finish_user_form');
      }
    }
  }

  public function executeStep2(sfWebRequest $request)
  {
    if (!($userId = (int)$this->getUser()->getFlash('new_user_id', 0)))
    {
      $this->redirect('@new_user_form');
    }

    $this->form = new ProfileForm();

    if ($request->isMethod('post'))
    {
      $profile = $request->getParameter('profile');
      $profile['first_name'] = trim(preg_replace('/\s{2,}/', ' ', $profile['first_name']));
      $profile['last_name'] = trim(preg_replace('/\s{2,}/', ' ', $profile['last_name']));
      $profile['user_id'] = $userId;
      $profile['plate'] = strtoupper(trim(preg_replace('/\s+/', '', $profile['plate'])));

      $this->form->bind($profile);
      if ($this->form->isValid())
      {
        $this->form->save();
        $this->redirect('@thank_you_for_registration');
      }
    }

    $this->getUser()->setFlash('new_user_id', $userId);
  }

  public function executeThankYou(sfWebRequest $request)
  {
  }
}
